package top.smallway.lightbandassistant_app.UI;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONObject;
import com.bumptech.glide.Glide;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.List;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.adapter.SongsAdapter;
import top.smallway.lightbandassistant_app.data.Song_Data;
import top.smallway.lightbandassistant_app.method.Method;
import top.smallway.lightbandassistant_app.network.Okhttp;

/**
 * 作者歌曲列表页面
 * 用于显示特定作者创作的所有歌曲
 */
public class Author_Songs extends AppCompatActivity {
    // 作者ID
    private String id;
    // 作者名称
    private String name;
    // 作者头像URL
    private String avatar;
    // 显示歌曲列表的RecyclerView
    private RecyclerView author_songs;
    // 显示作者名称和ID的TextView
    private TextView author_name, author_id;
    // 显示作者头像的ImageView
    private ImageView author_avater;
    // 网格布局管理器
    private GridLayoutManager gridLayoutManager;
    // 歌曲列表适配器
    private SongsAdapter songsAdapter;
    // 加载对话框
    private BasePopupView xPopup;


    /**
     * Activity生命周期的onCreate方法
     * 初始化页面并加载作者的歌曲数据
     * 1. 调用initview()初始化页面控件
     * 2. 显示加载对话框
     * 3. 在新线程中请求并加载作者的歌曲数据
     * 4. 数据加载完成后设置RecyclerView的布局和适配器
     *
     * @param savedInstanceState 保存的实例状态
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_songs);
        initview();
        xPopup = new XPopup.Builder(this).asLoading("正在加载中").show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = Okhttp.GetSongByAuthorId(id);
                List<Song_Data> data = Method.unmarshalSongDataList(jsonObject.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        gridLayoutManager = new GridLayoutManager(Author_Songs.this, 2);
                        author_songs.setLayoutManager(gridLayoutManager);
                        songsAdapter = new SongsAdapter(data, Author_Songs.this);
                        author_songs.setAdapter(songsAdapter);
                        xPopup.smartDismiss();
                    }
                });
            }
        }).start();

    }

    /**
     * 初始化页面视图
     * 包括初始化控件、设置作者信息和头像
     */
    /**
     * 初始化页面视图
     * 包括初始化控件、设置作者信息和头像
     * 1. 从Intent中获取作者ID、名称和头像URL
     * 2. 初始化页面上的所有控件
     * 3. 使用Glide加载作者头像
     * 4. 设置作者名称和ID的显示
     */
    private void initview() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        name = intent.getStringExtra("name");
        avatar = intent.getStringExtra("avatar");
        author_songs = findViewById(R.id.author_songs);
        author_name = findViewById(R.id.author_name);
        author_id = findViewById(R.id.author_id);
        author_avater = findViewById(R.id.author_avater);
        Glide.with(author_avater).load(avatar).into(author_avater);
        author_name.setText(name);
        author_id.setText(id);
    }
}